<?php

class Client
{    
    var $ID;
    var $lname =  '';
    var $fname =  '';
    var $company =  '';
    var $contactNo = '';  
    
    public function retrieve() 
    {    
    	require('../includes/DBConn.inc');
	$result = mysql_query("SELECT tblclient.ID, tblclient.Lname, 
		tblclient.Fname, tblclient.Company, tblclient.ContactNo, tblcompany.Company 
		FROM tblclient LEFT OUTER JOIN tblcompany 
		ON tblclient.Company = tblcompany.ID");
	return $result;        
    }
    
    public function retrieveCompanies() 
    {
	require('../includes/DBConn.inc');
	$result = mysql_query("SELECT * from tblcompany");
	return $result;        
    }
    
    public function retrieveByLName($searchVal) 
    {
	require('../includes/DBConn.inc');
	$result = mysql_query("SELECT tblclient.ID, tblclient.Lname, 
		tblclient.Fname, tblclient.Company, tblclient.ContactNo, tblcompany.Company 
		FROM tblclient LEFT OUTER JOIN tblcompany 
		ON tblclient.Company = tblcompany.ID WHERE Lname = '$searchVal'");
	return $result;        
    }
    
    public function retrieveByID($id) 
    {
	require('../includes/DBConn.inc');
	$result = mysql_query("SELECT * from tblclient WHERE ID=$id");
	$row = mysql_fetch_array($result);
	$this->lname =  $row['Lname'];
	$this->fname =  $row['Fname'];
	$this->company =  $row['Company'];
	$this->contactNo =  $row['ContactNo'];
    }
    
    public function add()
    {       
      	 require('../includes/DBConn.inc');
      	 $query = "INSERT INTO tblclient (lname, fname, company, contactno)
      	           VALUES ('$_POST[last]', '$_POST[first]', '$_POST[company]',
      	           '$_POST[contactNo]')";
      	 echo $_POST[company];
      	 $result = mysql_query($query) or die("Error: ".mysql_error());
      	 return $result;
    }
    
    public function update($id)
    {
      	 require('../includes/DBConn.inc');
      	 $query = "UPDATE tblclient SET lname='$_POST[last]', fname='$_POST[first]',
      	 	   company='$_POST[company]', contactno='$_POST[contactNo]' 
      	 	   WHERE ID=$id";
      	 	   
      	 $result = mysql_query($query) or die("Error: ".mysql_error());
      	 return $result;
    }
    
    public function delete($id)
    {
    	 require('../includes/DBConn.inc');
         $result = mysql_query("DELETE FROM tblclient WHERE id=$id") or die(mysql_error());   
    }
    
}
?>